class Admin::TagsController < AdminController
  before_filter :find_tag, :only => [:show, :edit, :update, :destroy]
  
  def index
    @tags = Tag.valid.filter(params).paginate :page => params[:page], :per_page => 30
  end
  
  def show
  end
  
  def new
    @tag = Tag.new
  end
  
  def create
    @tag = Tag.new(params[:tag])
    if @tag.save
      flash[:notice] = "Successfully created tag."
      redirect_to params[:save_and_new].blank? ? edit_admin_tag_path(@tag) : new_admin_tag_path
    else
      render 'new'
    end
  end
  
  def edit
  end
  
  def update
    if @tag.update_attributes(params[:tag])
      flash[:notice] = "Successfully updated tag."
      redirect_to params[:save_and_new].blank? ? edit_admin_tag_path(@tag) : new_admin_tag_path
    else
      render 'edit'
    end
  end
  
  def destroy
    @tag.del
    flash[:notice] = "Successfully destroyed tag."
    redirect_to admin_tags_url
  end
  
  def update_status
    if params[:ids].blank? or params[:ids].all?{|id| id.blank? }
      flash[:error] = "操作失败."
    else
      status = params[:status].blank? ? 0 : params[:status].to_i
      Tag.update_all("status = #{status}", "id IN (#{params[:ids].join(',')})")
      flash[:notice] = "操作成功."
    end
    redirect_to admin_tags_url
  end
  
  private

  def find_tag
    @tag = Tag.find(params[:id])
  end
end
